'use strict';

/**
 * @ngdoc function
 * @name youaBiDashboardAngularApp.controller:MainCtrl
 * @description
 * # MainCtrl
 * Controller of the youaBiDashboardAngularApp
 */
angular.module('youaBiDashboardAngularApp')
	.controller('EmployeeCtrl', function($scope, $http, $location) {
		this.awesomeThings = [
			'HTML5 Boilerplate',
			'AngularJS',
			'Karma'
		];

		var username = window.localStorage.getItem('username');
		var csrftoken = window.localStorage.getItem('csrftoken');

		if (!username || !csrftoken) {
			return $location.path('/login')
		}

		$http({
			method: 'POST',
			url: '/user/checkUserLogin',
			params: {
				username: username,
				csrftoken: csrftoken
			}
		}).then(function successCallback(response) {
			var result = response.data.result;
			if (!result) {
				window.localStorage.removeItem('username')
				window.localStorage.removeItem('csrftoken')

				return $location.path('/login')
			}
		})

		$scope.template = {
			sidebar: "views/sidebar.html",
			header: "views/header.html"
		}

		$scope.periodRadio = 'week';

		$scope.inlineOptions = {
			minDate: new Date(),
			showWeeks: true
		};

		$scope.dateOptions = {
			formatYear: 'yy',
			maxDate: new Date(2020, 5, 22),
			startingDay: 1
		};

		$scope.popup = {
			opened: false
		};

		$scope.isShowList = false;

		$scope.start = null;
		$scope.end = null;

		var getStartToEnd = function(period, date) {

			var map = {
				'day': function() {
					$scope.start = moment(date).format('YYYY-MM-DD');
					$scope.end = moment(date).format('YYYY-MM-DD');
				},

				'week': function() {
					$scope.start = moment(date).isoWeekday("Monday").format('YYYY-MM-DD');
					$scope.end = moment(date).isoWeekday("Sunday").format('YYYY-MM-DD')
				},

				'month': function() {
					var year = moment(date).format('YYYY');
					var month = moment(date).format('MM');
					var endday = getEndOfMonth(year, month);

					$scope.start = moment().year(year).month(parseInt(month) - 1).date(1).format('YYYY-MM-DD');
					$scope.end = moment().year(year).month(parseInt(month) - 1).date(endday).format('YYYY-MM-DD');
				}
			}

			var fn = map[period];
			if (_.isFunction(fn)) {
				fn()
			}
		}

		var getEndOfMonth = function(year, month) {
			var map = {
				'01': 31,
				'02': function(year, month) {
					if (year % 4 == 0) {
						return 29
					} else {
						return 28
					}
				},
				'03': 31,
				'04': 30,
				'05': 31,
				'06': 30,
				'07': 31,
				'08': 31,
				'09': 30,
				'10': 31,
				'11': 30,
				'12': 31
			}

			return _.isFunction(map[month]) ? map[month](year, month) : map[month];
		}

		$scope.format = 'yyyy-MM-dd';
		$scope.altInputFormats = 'yyyy-MM-dd';
		$scope.dt = Date.now();

		$scope.open = function() {
			$scope.popup.opened = true;
		}

		$scope.logout = function () {
			var username = window.localStorage.getItem('username');

			$http({
				method: 'POST',
				url: '/user/logout',
				params: {
					username: username
				}
			}).then(function successCallback(response) {
				var result = response.data.result;
				if (result) {
					window.localStorage.removeItem('username')
					window.localStorage.removeItem('csrftoken')

					return $location.path('/login')
				}
			})
		}

		$scope.fetch = function() {

			var period = $scope.periodRadio;
			var date = $scope.dt;

			if (!period || !date) {
				return;
			}

			getStartToEnd(period, _.isNumber(date) ? new Date(date) : date)

			$http({
				method: 'GET',
				url: '/kpi/list',
				params: {
					period: period,
					timestamp: _.isNumber(date) ? date : date.getTime()
				}
			}).then(function successCallback(response) {

				var results = response.data.result;

				if (results) {
					$scope.isShowList = true;
					var nresults = _.sortBy(results, function(value, key, list) {
						return -value.paid_payment_amount
					});

					$scope.kpis = nresults;
				} else {
					$scope.isShowList = false;
				}

			}, function errorCallback(response) {

			});
		}
	});